package com.mall.app.web;


import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.app.bean.TbUser;
import com.mall.app.service.TbUserService;
import com.mall.app.utils.MyAccount;
import com.mall.app.utils.MyToken;
/**
 * <p>
 * 商家表 前端控制器
 * </p>
 *
 * @author LeeSin
 * @since 2020-07-10
 */
@RestController
@RequestMapping("/user")
public class TbUserController {
	@Autowired
	private TbUserService userService;
	/**
	 * 后端类别分页方法
	 * @return
	 */
	
	@RequestMapping("/listPage")
	public Map<String,Object> listPage(int page,int limit,String uname){
		//设置查询条件
		QueryWrapper<TbUser> queryWrapper = new QueryWrapper<TbUser>();
		if(uname!=null){
			queryWrapper.like(true, "uname", uname);
		}
		
		
		Map<String,Object> result=new HashMap<String,Object>();
		result.put("code", 0);
		result.put("msg", "");
		result.put("count",userService.count(queryWrapper));
		result.put("data", userService.page(new Page<TbUser>(page,limit), queryWrapper).getRecords());
		return result;
	}
	
	@RequestMapping("/list")
	public List<TbUser> list(){		
		return userService.list(null);
	}
	
	@RequestMapping("/delete")
	public boolean removeUser(@RequestParam("uids[]")List<Integer> uids){
		return userService.removeByIds(uids);
	}

	@RequestMapping("/update")
	public boolean updateUser(TbUser user) {
		LocalDateTime now = LocalDateTime.now();
		user.setModifyTime(now);
		return userService.updateById(user);
	}
	
	@RequestMapping("/insert")
	public boolean insertUser(TbUser user) {
		LocalDateTime now = LocalDateTime.now();
		user.setModifyTime(now);
		user.setCreateTime(now);
		return userService.save(user);
	}
	
	@RequestMapping("/login")
	public String login(String username,String password){
		//1.将登录的用户名和密码封装到shiro的令牌类中
		
		password = new Md5Hash(password, username+password, 1000).toString();//加密
		System.out.println(password);
		MyToken token = new MyToken(username,password,"User");
		//2.根据抛出的异常来返回值
		Subject subject=SecurityUtils.getSubject();
		if(!subject.isAuthenticated()){
			//如果主体没有认证通过
			try {
				subject.login( token );//会跳转到自定义的MyRealm类中
			} catch ( UnknownAccountException uae ) {
				//用户名输入不正确
				System.out.println("用户名输入不正确");
				return "-1";
			} catch ( IncorrectCredentialsException ice ) {
				System.out.println("密码输入错误");
				return "-2";
			} catch (AuthenticationException e) {
				System.out.println("认证失败");
				e.printStackTrace();
				return "-3";
			}
		}
		System.out.println("登录成功");
		MyAccount account = (MyAccount)SecurityUtils.getSubject().getSession().getAttribute("account");
		if(account!=null) {
			System.out.println(account.getLoginType());
			System.out.println(account.getUsername());
		}
		else 
			System.out.println("no login");
		return "0";//登录成功
	}
}

